package com.test.springboot3.springboot3testdemo1.controller;

import static com.test.springboot3.springboot3testdemo1.config.KafkaConfig.SPRING_BOOT_TEST_TOPIC;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/8/6-下午4:10
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class KafkaMsgController {
  private final KafkaTemplate kafkaTemplate;

  @RequestMapping("/sendKafka")
  public String sendMsg(@RequestParam("msg") String msg) {
    kafkaTemplate.send(SPRING_BOOT_TEST_TOPIC, "test-key", msg);
    return "success";
  }

  @KafkaListener(topics = SPRING_BOOT_TEST_TOPIC)
  public void receiveMsg(String msg) {
    log.info("[KAFKA] receive msg: {}", msg);
  }
}
